package cn.ly.objdata;

public class DataArray {
	
	private Person[] arrp;
	private int nElems;
	
	public DataArray(int max){
		arrp = new Person[max];
		this.nElems = 0;
	}
	
	public Person find(Person p){
		int j = 0;
		for(; j < nElems ; j++){
			if( this.arrp[j].getLastName().equals(p.getLastName()) 
					&& this.arrp[j].getFirstName().equals(p.getFirstName()) 
							&& this.arrp[j].getAge() == p.getAge()){
				break;
			}
		}
		return arrp[j];
	}
	
	public void insert(Person p){
		this.arrp[this.nElems] = p;
		this.nElems++;
	}
	
	//排序，插入排序法
	public void insertSort(){
		Person temp = null;
		int j = 0;
		for(int i=1; i < this.nElems; i++){//i=1否则就，arrp[0-1]了
			j = i;
			temp = arrp[i];
			while(j > 0 && arrp[j-1].compare(temp) > 0){
				arrp[j] = arrp[j-1];
				j--;
			}
			arrp[j] = temp;
		}
	}
	
	public boolean delete(Person p){
		int j = 0;
		for(; j < nElems ; j++){
			if( this.arrp[j].getLastName().equals(p.getLastName()) 
					&& this.arrp[j].getFirstName().equals(p.getFirstName()) 
							&& this.arrp[j].getAge() == p.getAge()){
				break;
			}
		}
		if( j < this.nElems){
			for(int k = j; k < this.nElems; k++){
				this.arrp[k]  = this.arrp[k+1];
			}
			this.nElems--;
			return true;
		}
		return false;
	}
	
	public void displayArr(){
		for(int j = 0; j < this.nElems; j++){
			this.arrp[j].displayPerson();
		}
		System.out.println();
	}

}
